package com.cqengine.entity;

import com.cqengine.util.DateUtil;
import com.googlecode.cqengine.attribute.SimpleAttribute;
import com.googlecode.cqengine.query.option.QueryOptions;
import lombok.*;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @author deng
 * id
 * userId
 * price
 * qty
 * symbol
 * isTrigger 是否触发
 * type  类型
 * TS
 * timeout
 * retryTime  重复次数
 */
@Data
@Slf4j
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Position implements Cloneable {

    private String id;
    private String userId;
    private double price;
    private double qty;
    private String symbol;
    private Boolean isTriggered = false;
    /**
     * 1.强平 2.风控 3.止盈  4.止损
     * */
    private Integer type;
    /**
     * 执行完成时间
     */
    private String ts;
    private String timeout = "3000";
    private Integer retryTime = 3;
    static final AtomicLong VERSION_GENERATOR = new AtomicLong();
    final long version = VERSION_GENERATOR.incrementAndGet();

    public String getKey(){
        return  type + ":" + userId + ":" + symbol ;
    }

    @Override
    public int hashCode() {
        return Integer.valueOf(id);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Position other = (Position) o;
        if (this.id != other.id) {
            return false;
        }
        if (this.version != other.version) {
            return false;
        }
        return true;
    }

    public boolean verifyPosition(){
        if(userId == null || userId.isEmpty()){
            log.info("userID is empty.");
            return false;
        }
        if(price <= 0){
            log.info("price cannot be less than or equal to zero.");
            return false;
        }
        if(qty <= 0){
            log.info("qty cannot be less than or equal to zero.");
            return false;
        }
        if(symbol == null || symbol.isEmpty()){
            log.info("symbol cannot be less than or equal to zero.");
            return false;
        }
        return true;
    }


    public static final SimpleAttribute<Position, String> ID = new SimpleAttribute<Position, String>("id") {
        @Override
        public String getValue(Position position, QueryOptions queryOptions) { return position.id; }
    };

    public static final SimpleAttribute<Position, String> USERID = new SimpleAttribute<Position, String>("userId") {
        @Override
        public String getValue(Position position, QueryOptions queryOptions) { return position.userId; }
    };

    public static final SimpleAttribute<Position, Double> PRICE = new SimpleAttribute<Position, Double>("price") {
        @Override
        public Double getValue(Position position, QueryOptions queryOptions) { return position.price; }
    };

    public static final SimpleAttribute<Position, Double> QTY = new SimpleAttribute<Position, Double>("qty") {
        @Override
        public Double getValue(Position position, QueryOptions queryOptions) { return position.qty; }
    };

    public static final SimpleAttribute<Position, String> SYMBOL = new SimpleAttribute<Position, String>("symbol") {
        @Override
        public String getValue(Position position, QueryOptions queryOptions) { return position.symbol; }
    };

    public static final SimpleAttribute<Position, Boolean> IS_TRIGGER = new SimpleAttribute<Position, Boolean>("isTriggered") {
        @Override
        public Boolean getValue(Position position, QueryOptions queryOptions) { return position.isTriggered; }
    };

    public static final SimpleAttribute<Position, Integer> TYPE = new SimpleAttribute<Position, Integer>("type") {
        @Override
        public Integer getValue(Position position, QueryOptions queryOptions) { return position.type; }
    };

    public static final SimpleAttribute<Position, String> TS = new SimpleAttribute<Position, String>("ts") {
        @Override
        public String getValue(Position position, QueryOptions queryOptions) { return position.ts; }
    };

    public static final SimpleAttribute<Position, String> TIME_OUT = new SimpleAttribute<Position, String>("timeout") {
        @Override
        public String getValue(Position position, QueryOptions queryOptions) { return position.timeout; }
    };

    public static final SimpleAttribute<Position, Integer> RETRY_TIME = new SimpleAttribute<Position, Integer>("retryTime") {
        @Override
        public Integer getValue(Position position, QueryOptions queryOptions) { return position.retryTime; }
    };

    @Override
    public Position clone() {
        Position position = new Position();
        position.setId(this.getId());
        position.setUserId(this.getUserId());
        position.setPrice(this.getPrice());
        position.setQty(this.getQty());
        position.setSymbol(this.getSymbol());
        position.setIsTriggered(this.getIsTriggered());
        position.setType(this.getType());
        position.setTs(this.getTs());
        position.setTimeout(this.getTimeout());
        position.setRetryTime(this.getRetryTime());
        return position;

    }
}
